package com.ruoyi.hqfinance.controller;

import cn.hutool.core.net.URLEncoder;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.hqfinance.domain.HqClientRelation;
import com.ruoyi.hqfinance.domain.dto.HqClientRelationDto;
import com.ruoyi.hqfinance.domain.vo.HqClientRelationVo;
import com.ruoyi.hqfinance.mapper.HqClientRelationMapper;
import com.ruoyi.hqfinance.service.HqClientRelationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.nio.charset.Charset;
import java.util.List;

@RestController
@RequestMapping("/hq/clientRelation")
@Api(tags = "客户配置")
public class HqClientRelationController {

    @Autowired
    private HqClientRelationService hqClientRelationService;
    @Autowired
    private HttpServletResponse response;


    @ApiOperation("搜索客户")
    @PostMapping("/search")
    public R search(@RequestBody HqClientRelationVo vo) {
        if (vo == null){
            return R.ok(hqClientRelationService.selectAll());
        }
        List<HqClientRelation> list = hqClientRelationService.search(vo);
        if (list == null || list.size() == 0) {
            return R.fail("暂无数据");
        } else {
            return R.ok(list);
        }
    }

    @ApiOperation("查父客户")
    @GetMapping("/selectBigClient")
    public R selectBigClient() {
        List<HqClientRelation> list = hqClientRelationService.selectBigClient();
        if (list == null || list.size() == 0) {
            return R.ok("暂无数据");
        } else {
            return R.ok(list);
        }
    }

    @ApiOperation("添加大客户")
    @PostMapping("insertBigClient")
    public R insertBigClient(@RequestBody HqClientRelation hqClientRelation){
        if (hqClientRelation.getClientName().equals(hqClientRelationService.findParentName(hqClientRelation.getClientName()))){
            return R.fail("添加失败，父客户:" + hqClientRelation.getClientName() + "已经存在");
        }
        Boolean isAdd = hqClientRelationService.insertBigClient(hqClientRelation);
        if (isAdd){
            return R.ok("添加成功");
        }else {
            return R.fail("添加失败");
        }
    }
    @ApiOperation("添加子客户")
    @PostMapping("insertSmallClient")
    public R insertSmallClient(@RequestBody HqClientRelation hqClientRelation){
        if (hqClientRelation.getClientName().equals(hqClientRelationService.findSonName(hqClientRelation.getClientName()))){
            return R.fail("添加失败，子客户:" + hqClientRelation.getClientName() + "已经存在");
        }
        Boolean isAdd = hqClientRelationService.insertSmallClient(hqClientRelation);
        if (isAdd){
            return R.ok("添加成功");
        }else {
            return R.fail();
        }
    }

    @ApiOperation("修改客户信息")
    @PostMapping("update")
    public R update(@RequestBody HqClientRelation hqClientRelation){
        Boolean isAdd = hqClientRelationService.update(hqClientRelation);
        if (isAdd){
            return R.ok("修改成功");
        }else {
            return R.fail("修改失败");
        }
    }

    @ApiOperation("删除客户信息")
    @PostMapping("deleteById/{id}")
    public R deleteById(@PathVariable("id") Integer id){
        Boolean isAdd = hqClientRelationService.deleteById(id);
        if (isAdd){
            return R.ok("删除成功");
        }else {
            return R.fail("删除失败");
        }
    }

    @ApiOperation("导出")
    @GetMapping("/export")
    public void export() {
        String fileName = "attachment;filename=" + URLEncoder.createDefault().encode("客户配置", Charset.forName("utf-8")) + ".xlsx";
        response.setHeader("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", fileName);
        response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
        List<HqClientRelationDto> list = hqClientRelationService.export();
        ExcelUtil<HqClientRelationDto> util = new ExcelUtil(HqClientRelationDto.class);
        util.exportExcel(response, list, "客户配置");
    }

}
